package org.gw.building.repositories;

import org.gw.building.model.Term;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface TermRepository extends CrudRepository<Term, String> {
	/**
	 * 查询期次
	 * 
	 * @param name
	 * @param openStu
	 * @param status
	 * @param trainType
	 * @param pageable
	 * @return
	 */
	@Query("from Term t where (?1 is null or t.name like ?1) and (?2 is null or t.openStu = ?2) and (?3 is null or t.status=?3) and (?4 is null or t.trainType.id = ?4)  order by t.status desc, t.qs desc,t.pubTime desc ")
	public Page<Term> query(String name, Integer openStu, Integer status, String trainType, Pageable pageable);

	/**
	 * 获取当前开启的期次
	 * 
	 * @return
	 */
	@Query("from Term t where t.status=1 and  t.trainType.id=?1")
	public Term getCurrentTerm(String trainType);

	@Modifying
	@Query("update Term t set t.status=0 where t.trainType.id=?1")
	public int disableAll(String trainType);

	@Query("select max(t.qs) from Term t  where t.trainType.id=?1")
	public int getMaxQsFromTerm(String trainType);
}
